﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HenIT.Data.SqlClient;
using System.Data.SqlClient;
using System.Data;
namespace EventScavenger
{
    public partial class EventScavengerDAL : GenericSQLServerDAL
    {
        private T ReadSettingValue<T>(object dvalue, T defaultValue)
        {
            T returnValue = defaultValue;
            if (dvalue != DBNull.Value)
                returnValue = (T)dvalue;
            return returnValue;
        }
        private DateTime ReadSettingValueDate(object dvalue, DateTime defaultValue)
        {
            DateTime returnValue = defaultValue;
            if (dvalue != DBNull.Value)
                returnValue = DateTime.Parse(dvalue.ToString());
            return returnValue;
        }
        internal GlobalSettingsData GetGlobalSettings()
        {
            GlobalSettingsData globalSettingsData = new GlobalSettingsData();
            string sql = "SelectGlobalSettings";
            DataSet ds = base.GetDataSet(sql, CommandType.StoredProcedure);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                globalSettingsData.MaxLogEntries = ReadSettingValue<int>(r["MaxLogEntries"], globalSettingsData.MaxLogEntries);
                globalSettingsData.MaintenanceEnabled = ReadSettingValue<bool>(r["MaintenanceEnabled"], globalSettingsData.MaintenanceEnabled);
                globalSettingsData.MaintenanceFrequencyMinutes = ReadSettingValue<int>(r["MaintenanceFrequencyMinutes"], globalSettingsData.MaintenanceFrequencyMinutes);
                globalSettingsData.MaintenanceRunner = ReadSettingValue<string>(r["MaintenanceRunner"].ToString(), globalSettingsData.MaintenanceRunner);
                globalSettingsData.MaintenanceLastRun = ReadSettingValueDate(r["MaintenanceLastRun"], globalSettingsData.MaintenanceLastRun);
                globalSettingsData.MaintenanceTimeOutSeconds = ReadSettingValue(r["MaintenanceTimeOutSeconds"], globalSettingsData.MaintenanceTimeOutSeconds);
                globalSettingsData.NextCreateHistoryTime = ReadSettingValueDate(r["NextCreateHistoryTime"], globalSettingsData.NextCreateHistoryTime);
                globalSettingsData.RunCreateHistoryEnabled = ReadSettingValue<bool>(r["RunCreateHistoryEnabled"], globalSettingsData.RunCreateHistoryEnabled);
                globalSettingsData.NextCreateHistoryFrequencyMinutes = ReadSettingValue<int>(r["NextCreateHistoryFrequencyMinutes"], globalSettingsData.NextCreateHistoryFrequencyMinutes);
                globalSettingsData.DaysToHistory = ReadSettingValue<int>(r["DaysToHistory"], globalSettingsData.DaysToHistory);
                globalSettingsData.RepeatTimeSec = ReadSettingValue<int>(r["RepeatTimeSec"], globalSettingsData.RepeatTimeSec);
            }
            return globalSettingsData;
        }
        internal void SetGlobalSettings(GlobalSettingsData globalsettings)
        {
            string sql = "UpdateGlobalSettings";
            SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@MaxLogEntries", globalsettings.MaxLogEntries),
					new SqlParameter("@MaintenanceEnabled", globalsettings.MaintenanceEnabled),
					new SqlParameter("@MaintenanceFrequencyMinutes", globalsettings.MaintenanceFrequencyMinutes),
					new SqlParameter("@MaintenanceRunner", globalsettings.MaintenanceRunner),
					new SqlParameter("@NextCreateHistoryTime", globalsettings.NextCreateHistoryTime.ToString("yyyy-MM-dd HH:mm")),
					new SqlParameter("@RunCreateHistoryEnabled", globalsettings.RunCreateHistoryEnabled),
					new SqlParameter("@NextCreateHistoryFrequencyMinutes", globalsettings.NextCreateHistoryFrequencyMinutes),
					new SqlParameter("@DaysToHistory", globalsettings.DaysToHistory),
					new SqlParameter("@RepeatTimeSec", globalsettings.RepeatTimeSec),
					new SqlParameter("@MaintenanceTimeOutSeconds", globalsettings.MaintenanceTimeOutSeconds)
				};
            ExecuteSP(sql, parameters);
        }
    }
}
